Skip to content

Method: static {...}

1: /*
2: * Copyright © 2021 Fachhochschule für die Wirtschaft (FHDW) Hannover
3: *
4: * This file is part of ipspiel23-tictactoe-core.
5: *
6: * ipspiel23-tictactoe-core is free software: you can redistribute it and/or modify it under
7: * the terms of the GNU General Public License as published by the Free Software
8: * Foundation, either version 3 of the License, or (at your option) any later
9: * version.
10: *
11: * ipspiel23-tictactoe-core is distributed in the hope that it will be useful, but WITHOUT
12: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14: * details.
15: *
16: * You should have received a copy of the GNU General Public License along with
17: * ipspiel23-tictactoe-core. If not, see <http://www.gnu.org/licenses/>.
18: */
19: package de.fhdw.gaming.ipspiel23.tictactoe.core.domain.impl;
20:
21: import java.util.ArrayList;
22: import java.util.List;
23: import java.util.Map;
24: import java.util.Optional;
25: import java.util.Random;
26:
27: import de.fhdw.gaming.core.domain.DefaultGame;
28: import de.fhdw.gaming.core.domain.ObserverFactoryProvider;
29: import de.fhdw.gaming.ipspiel23.tictactoe.core.domain.TicTacToeField;
30: import de.fhdw.gaming.ipspiel23.tictactoe.core.domain.TicTacToeFieldState;
31: import de.fhdw.gaming.ipspiel23.tictactoe.core.domain.TicTacToeGame;
32: import de.fhdw.gaming.ipspiel23.tictactoe.core.domain.TicTacToeMoveChecker;
33: import de.fhdw.gaming.ipspiel23.tictactoe.core.domain.TicTacToePlayer;
34: import de.fhdw.gaming.ipspiel23.tictactoe.core.domain.TicTacToeState;
35: import de.fhdw.gaming.ipspiel23.tictactoe.core.domain.TicTacToeStrategy;
36: import de.fhdw.gaming.ipspiel23.tictactoe.core.moves.TicTacToeMove;
37: import de.fhdw.gaming.ipspiel23.tictactoe.core.moves.factory.TicTacToeMoveFactory;
38: import de.fhdw.gaming.ipspiel23.tictactoe.core.moves.impl.TicTacToeDefaultMoveFactory;
39:
40: /**
41: * Implements the Tic Tac Toe game.
42: */
43: final class TicTacToeGameImpl extends DefaultGame<TicTacToePlayer, TicTacToeState, TicTacToeMove, TicTacToeStrategy>
44: implements TicTacToeGame {
45:
46: /**
47: * The random number generator.
48: */
49: private static final Random RANDOM = new Random();
50:
51: /**
52: * The number of rows (and columns) of the board.
53: */
54: private final int boardSize;
55:
56: /**
57: * The move factory.
58: */
59: private final TicTacToeMoveFactory moveFactory;
60:
61: /**
62: * Creates a TicTacToe game.
63: *
64: * @param id The ID of this game.
65: * @param initialState The initial state of the game.
66: * @param strategies The players' strategies.
67: * @param maxComputationTimePerMove The maximum computation time per move in seconds.
68: * @param moveChecker The move checker.
69: * @param observerFactoryProvider The {@link ObserverFactoryProvider}.
70: * @throws IllegalArgumentException if the player sets do not match.
71: * @throws InterruptedException if creating the game has been interrupted.
72: */
73: TicTacToeGameImpl(final int id, final TicTacToeState initialState, final Map<String, TicTacToeStrategy> strategies,
74: final long maxComputationTimePerMove, final TicTacToeMoveChecker moveChecker,
75: final ObserverFactoryProvider observerFactoryProvider)
76: throws IllegalArgumentException, InterruptedException {
77:
78: super(id, initialState, strategies, maxComputationTimePerMove, moveChecker, observerFactoryProvider);
79: this.boardSize = initialState.getBoard().getSize();
80: this.moveFactory = new TicTacToeDefaultMoveFactory();
81: }
82:
83: @Override
84: public Optional<TicTacToeMove> chooseRandomMove(final TicTacToePlayer player, final TicTacToeState state) {
85: final List<TicTacToeField> fields = new ArrayList<>(
86: state.getBoard().getFieldsBeing(TicTacToeFieldState.EMPTY).values());
87:
88: if (fields.isEmpty()) {
89: return Optional.empty();
90: }
91: final int index = RANDOM.nextInt(fields.size());
92: final TicTacToeField field = fields.get(index);
93: return Optional.of(this.moveFactory.createPlaceMarkMove(player.isUsingCrosses(), field.getPosition()));
94: }
95:
96: @Override
97: public String toString() {
98: return String
99: .format("TicTacToeGame[id=%d, boardSize=%d, %s]", this.getId(), this.boardSize, this.gameToString());
100: }
101:
102: @Override
103: public int getBoardSize() {
104: return this.boardSize;
105: }
106: }